package com.woniuxy.controller;

import com.woniuxy.dao.ProductRepo;
import com.woniuxy.entity.*;
import com.woniuxy.service.LiuService;
import com.woniuxy.util.QiniuCloudUtil;
import com.woniuxy.util.Result;
import com.woniuxy.vo.ProductVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Time;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/*
@author Jan
@create 2020-05-21 18:06

*/
@RestController
public class ProductContrLiu {
    @Resource
    private LiuService liuServiceImp;
    @Resource
    private ProductRepo productRepo;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /*
    *查询该类别id的所有商品,含升序降序
    * price 只能是asc 或 desc
    * asc升序
    * desc降序
    * pageIndex当前页
    * 每页默认为6条
     */
    @RequestMapping("/selectAllProduct")
    @CrossOrigin
    public Result selectAllProduct(int categoryId,String price,int pageIndex){
        Map map=new HashMap();
        map.put("categoryId",categoryId);
        map.put("price",price);
        map.put("pageIndex",pageIndex);
        return new Result("200","success",liuServiceImp.selectAllProduct(map),null);
    }

    //查询集市商品详情需要proId ,categoryId
    @RequestMapping("/selectProductByProId")
    @CrossOrigin
    public Result selectProductByProId(int proId,int categoryId){
        Map map=new HashMap();
        map.put("categoryId",categoryId);
        map.put("proId",proId);
        Product product = liuServiceImp.selectProductByProId(map);
        return new Result("200","success",product,null);
    }
    //查询该proId的所有评论
    @RequestMapping("/selectAllProductCommentByProId")
    @CrossOrigin
    public Result selectAllProductCommentByProId(int proId,int pageIndex){
        HashMap map = new HashMap();
        map.put("proId",proId);
        map.put("pageIndex",pageIndex);
        List<ProductComment> list = liuServiceImp.selectAllCommentByProId(map);
        return new Result("200","success",null,list);
    }

    //查询该商品是否收藏
    @RequestMapping("/judgeUserCollection")
    @CrossOrigin
    public Result judgeUserCollection(int proId,int userId){
        JsCollection collection = liuServiceImp.judgeUserCollection(proId, userId);
        return new Result("200","success",collection,null);
    }
    //添加收藏
    @RequestMapping("/addCollection")
    @CrossOrigin
    public Result addCollection(JsCollection jsCollection){
        int i = liuServiceImp.AddCollection(jsCollection);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //取消收藏
    @CrossOrigin
    @RequestMapping("/deleteCollection")
    public Result deleteCollection(int proId,int userId){
        int i = liuServiceImp.deleteCollection(proId, userId);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //详情页面直接加入购物车
    @CrossOrigin
    @RequestMapping("/insertProductShoppingCar")
    public Result insertProductShoppingCar(int proId,int userId,int number){
        int i = liuServiceImp.insertProductShoppingCar(userId, proId, number);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //查询所有用户地址
    @CrossOrigin
    @RequestMapping("/selectAllUserAddressByUserId")
    public Result selectAllUserAddressByUserId(int userId){
        List<UserAddress> list = liuServiceImp.selectAllUserAddressByUserId(userId);
        return new Result("200","success",null,list);
    }

    //修改用户默认地址
    @CrossOrigin
    @RequestMapping("/updateUserMorenAddress")
    public Result updateUserMorenAddress(int newAddress,int oldAddress){
        int i = liuServiceImp.updateuserMorenAddress(newAddress, oldAddress);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //查询用户默认地址
    @CrossOrigin
    @RequestMapping("/selectUserMorenAddress")
    public Result selectUserMorenAddress(int  userId){
        UserAddress address = liuServiceImp.selectUserMorenAddress(userId);
        return  new Result("200","success",address,null);
    }

    //用户添加新地址
    @RequestMapping("/addUserNewAddress")
    @CrossOrigin
    public Result addUserNewAddress(int userId,String name,String address,String tel,String ifMoRen){
        int i = liuServiceImp.addUserAddress(userId, name, tel, address, ifMoRen);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //根据addressid删除地址
    @CrossOrigin
    @RequestMapping("/deleteAddress")
    public Result deleteAddress(int addressId){
        int i = liuServiceImp.deleteAddress(addressId);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //根据UserAddress对象更新地址
    @RequestMapping("/updateUserAddress")
    @CrossOrigin
    public Result updateUserAddress(UserAddress userAddress){
        int i = liuServiceImp.updateUserAddress(userAddress);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //查询改用户购物车信息
    @CrossOrigin
    @RequestMapping("/selectShoppingCar")
    public Result selectShoppingCar(int userId){
        List<JsShoppingCar> list = liuServiceImp.selectProductShoppingCarByUserId(userId);
        return new Result("200","success",null,list);
    }

    //根据对象修改购物车页面更改购物车商品数量
    @CrossOrigin
    @RequestMapping("/updateShoppingCar")
    public Result updateShoppingCar(JsShoppingCar jsShoppingCar){
        int i = liuServiceImp.updateShoppingCar(jsShoppingCar);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //根据id删除购物车
    @CrossOrigin
    @RequestMapping("/deleteShoppingCarById")
    public Result deleteShoppingCarById(int id){
        int i = liuServiceImp.deleteShoppingCar(id);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //查询该用户集市所有收藏
    @CrossOrigin
    @RequestMapping("/selectAllCollection")
    public Result selectAllCollection(int userId){
        List<JsCollection> list = liuServiceImp.selectAllCollection(userId);
        return new Result("200","success",null,list);
    }
    //根据id删除收藏
    @CrossOrigin
    @RequestMapping("/deleteCollectionById")
    public Result deleteCollectionById(int id){
        int i = liuServiceImp.deleteCollectionById(id);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }

    //查询该用户所有订单详情userId,status(条件查询)0位查询所有,1未付款,2待发货,3已发货,4交易完成
    //product_order中的ifcommnt是用来定义是该订单用户是否可见,1位可见,0为不可见(即该订单被删除时改为0)
    @CrossOrigin
    @RequestMapping("/selectAllOrederList")
    public Result selectAllOrederList(int userId,int status){
        List<ProductOrder> list = liuServiceImp.selectAllProductOrder(userId, status);
        return new Result("200","success",null,list);
    }

    //删除订单
    @CrossOrigin
    @RequestMapping("/deleteProductOrder")
    public Result deleteProductOrder(int productOrderId){
        liuServiceImp.updateProductOrderIfCommentToZero(productOrderId);
        return new Result("200","success",null,null);
    }

    //改变订单状态
    @CrossOrigin
    @RequestMapping("/updateProductOrderStatus")
    public Result updateProductOrderStatus(int proOrderId,int status){
        int i = liuServiceImp.updateProductOrderStatus(proOrderId, status);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //改变订单详情product_order_detail已评价ifcomment为1
    @CrossOrigin
    @RequestMapping("/updateProductOrderDetailIfcomment")
    public Result updateProductOrderDetailIfcomment(int id){
        int i = liuServiceImp.updateProductOrderDetailIfcomment(id);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //增加评论
    @CrossOrigin
    @RequestMapping("/insertProductComment")
    public Result insertProductComment(int proId,int userId,String comment){
        int i = liuServiceImp.insertProductComment(proId, userId, new Date(), comment);
        if (i==1){
            return new Result("200","success",null,null);
        }else{
            return new Result("400","defeat",null,null);
        }
    }
    //搜索查询
    @CrossOrigin
    @RequestMapping("/selectProductVo")
    public Result selectProductVo(String text){
        List<ProductVo> list = productRepo.selectProductVoByFenYe(text);
        return new Result("200","success",null,list);
    }

    //生成订单,未付款
    @CrossOrigin
    @RequestMapping("/addNewProductOrder")
    public Result addNewProductOrder(ProductOrder productOrder){
        productOrder.setIfcomment(1);
        productOrder.setStatus(1);
        productOrder.setTime(new Date());
        liuServiceImp.addProductOrderGetKey(productOrder);
        int proOrderId=productOrder.getProOrderId();
        System.out.println(proOrderId);
        //定时任务
        ValueOperations ops = stringRedisTemplate.opsForValue();
        ops.set("product"+productOrder.getProOrderId(),productOrder.getTime().toString(),3, TimeUnit.MINUTES);
        return new Result("200","success",proOrderId,null);
    }

    //生成订单商品详情
    @CrossOrigin
    @RequestMapping("/addProductOrderDetail")
    public Result addProductOrderDetail(ProductOrderDetail productOrderDetail){
        productOrderDetail.setIfcomment(0);
        liuServiceImp.addProductOrderDetail(productOrderDetail);
        return new Result("200","success",null,null);
    }
    @CrossOrigin
    @RequestMapping("/uploadImgs")
    public String uploadImgs(MultipartFile file) {
        MultipartFile avatar = file;
        System.out.println(file.getSize());
        System.out.println("参数：" + avatar);
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "_" + avatar.getOriginalFilename();
        try {
            System.out.println("文件名：" + fileName);
            QiniuCloudUtil.upload(fileName, avatar.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(QiniuCloudUtil.fullPath(fileName)+"============");
        //返回最终存入数据库的地址
        return QiniuCloudUtil.fullPath(fileName);
    }

    //确定付款改变订单状态为2
    @RequestMapping("/updateProductOrderStatusTwo")
    @CrossOrigin
    public Result updateProductOrderStatusTwo(int proOrderId){
        ProductOrder order = new ProductOrder();
        order.setStatus(2);
        order.setProOrderId(proOrderId);
        int i = liuServiceImp.updateProductOrderStatusTwo(order);
        //删除定时任务
        stringRedisTemplate.delete("product"+proOrderId);
        return new Result("200","success",null,null);
    }

    //减库存
    @CrossOrigin
    @RequestMapping("/updateProductStock")
    public Result updateProductStock(int proId,int number){
        liuServiceImp.updateProductStock(proId,number);
        return new Result("200","success",null,null);
    }

}
